using DM_BLL;
using DM_DAL;
using DM_IBLL;
using DM_IDAL;
using DM_Sqlite;
using DM_TOOLS.Tools.DBC.DBCCSV;
using DM_TOOLS.Tools.DBC.DBCProcess;
using DM_TOOLS.Tools.DBC.HEX;
using DMSystem_Sqlite_WebAPI.Filters;
using DMSystem_Sqlite_WebAPI.Utility;
using DMSystem_Sqlite_WebAPI.Utility.Quartz;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Quartz;
using Serilog;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
var configstr = new ConfigurationBuilder()
                 .SetBasePath(Directory.GetCurrentDirectory())
                 .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                 .Build();
string? sqlconfig = configstr.GetConnectionString("DataMonitor_SqliteConnection");
string[]? ipstr = configstr.GetSection("Localhost").Value?.Split(",");
//// 直接注入 Dictionary<string,List<string>>
//builder.Services.AddSingleton(provider =>
//{
//    var config = builder.Configuration.GetSection("SignalAliasMap")
//                  .Get<Dictionary<string, List<string>>>() ?? new();
//    return config;
//});
builder.Services.AddDbContext<MyDbContext>(options =>
{
    if (sqlconfig != null)
        options.UseSqlite(sqlconfig);
});
builder.Services.AddCors(opt =>
{

    opt.AddPolicy("dmsqlitereact", p =>
    {
        if (ipstr != null)
            p.AllowAnyHeader().AllowAnyMethod().WithOrigins(ipstr).AllowCredentials(); ;
    });
});
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
    .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, option =>
    {
        option.Cookie.SameSite = SameSiteMode.None;
        option.Cookie.SecurePolicy = CookieSecurePolicy.Always;
        option.AccessDeniedPath = "/Login/NoAuthResult";
    });
builder.Services.AddScoped<DbContext, MyDbContext>();
builder.Services.AddTransient<IDAL_Login, DAL_Login>();
builder.Services.AddTransient<IBLL_Login, BLL_Login>();

builder.Services.AddTransient<IDAL_Admin, DAL_Admin>();
builder.Services.AddTransient<IBLL_Admin, BLL_Admin>();

builder.Services.AddTransient<IDAL_SystemVersion, DAL_SystemVersion>();
builder.Services.AddTransient<IBLL_SystemVersion, BLL_SystemVersion>();

builder.Services.AddTransient<IDAL_User, DAL_User>();
builder.Services.AddTransient<IBLL_User, BLL_User>();

builder.Services.AddTransient<IDAL_DBCUser, DAL_DBCUser>();
builder.Services.AddTransient<IBLL_DBCUser, BLL_DBCUser>();

builder.Services.AddScoped<GlobalExceptionFilter>();
builder.Services.AddScoped<GlobalResponseWrapperFilter>();
builder.Services.AddMemoryCache();
builder.Services.AddScoped<MemoryCache>();
//builder.Services.AddScoped<FileWatcherHelper>();
builder.Services.AddHttpContextAccessor();
builder.Services.AddQuartz();
builder.Services.AddTransient<QuartzService>();
builder.Services.AddSignalR(options =>
{
    options.EnableDetailedErrors = true;
    options.ClientTimeoutInterval = TimeSpan.FromSeconds(30);
    options.KeepAliveInterval = TimeSpan.FromSeconds(15);
});


builder.Services.AddControllers(options =>
{
    // 注册全局异常过滤器
    options.Filters.AddService<GlobalExceptionFilter>();
    options.Filters.AddService<GlobalResponseWrapperFilter>();
});



// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(configstr)
    .CreateLogger();
builder.Host.UseSerilog(Log.Logger, true);
builder.Services.AddSerilog();



var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseSerilogRequestLogging();
app.UseCors("dmsqlitereact");
app.UseStaticFiles();
app.UseAuthentication();
app.UseAuthorization();
app.MapHub<SiginalrHub>("/SiginalrHub");
app.MapControllers();
app.Run();
